<?php 
class Sgb {  

    private $cssFiles = array();
    private $jsFiles = array();

    private $compressJsFiles = array();
    private $compressCssFiles = array();

    public function __construct() {
         
    }
    /**
     * 增加css文件到最前面
     * 
     **/
    public function prependCss($cssFiles, $compress = false){
        if(empty($cssFiles)){
            return;
        }
        if(!is_array($cssFiles)){
            $cssFiles = (array) $cssFiles;
        }
        foreach($cssFiles as $index => $file){
            $file = $cssFiles[$index] = trim($file);
            if(in_array($file, $this->cssFiles)){
                unset($cssFiles[$index]);
            }
        }
        if(!empty($cssFiles)){
            $this->cssFiles = array_merge($cssFiles, $this->cssFiles);
            if($compress){
                $this->compressCssFiles = array_merge($cssFiles, $this->compressCssFiles);
            }
        }
    }

    /**
     * 增加js文件到最前面
     * 
     **/
    public function prependJs($jsFiles, $compress = false){
        if(empty($jsFiles)){
            return;
        }
        if(!is_array($jsFiles)){
            $jsFiles = (array) $jsFiles;
        }
        foreach($jsFiles as $index => $file){
            $file = $jsFiles[$index] = trim($file);
            if(in_array($file, $this->jsFiles)){
                unset($jsFiles[$index]);
            }
        }
        if(!empty($jsFiles)){
            $this->jsFiles = array_merge($jsFiles, $this->jsFiles);
            if($compress){
                $this->compressJsFiles = array_merge($jsFiles, $this->compressJsFiles);
            }
        }
    }

    /**
     * 增加css文件
     * 
     */
    function addCss($css, $compress = false){ 
        $css = trim($css);
        if($css && !in_array($css, $this->cssFiles)){
            $this->cssFiles[] = $css;
            if($compress){
                $this->compressCssFiles[] = $css;
            }
        }
    }

    /**
     * 增加js文件
     * 
     */
    function addJs($js, $compress = false){
        $js = trim($js);
        if($js && !in_array($js, $this->jsFiles)){
            $this->jsFiles[] = $js;
            if($compress){
                $this->compressJsFiles[] = $js;
            }
        }
    }

    /**
     * 获取css文件
     * 
     **/
    function getCssFiles() {
        return $this->cssFiles;
    }

    /**
     * 获取js文件
     * 
     **/
    function getJsFiles() {
        return $this->jsFiles;
    }

    
    /**
     * 将email部分字符转换成 *
     * 
     **/
    function maskEmail($email) {
        if (strpos ( $email, '@' ) > 4) {
            $dot = "";
            for($i = 0; $i < strpos ( $email, '@' ) - 4; $i ++) {
                $dot .= "*";
            }
            return substr ( $email, 0, 3 ) . $dot . substr ( $email, strpos ( $email, '@' ) - 1 );
        } else if (strpos ( $email, '@' ) == 1) {
            return substr ( $email, 0, 0 ) . '*' . substr ( $email, strpos ( $email, '@' ) );
        } else if (strpos ( $email, '@' ) == 2) {
            return substr ( $email, 0, 1 ) . '*' . substr ( $email, strpos ( $email, '@' ) );
        } else if (strpos ( $email, '@' ) == 3) {
            return substr ( $email, 0, 1 ) . '*' . substr ( $email, strpos ( $email, '@' ) - 1 );
        } else if (strpos ( $email, '@' ) == 4) {
            return substr ( $email, 0, 2 ) . '*' . substr ( $email, strpos ( $email, '@')-1);
       }
    }

    /**
     * 返回包含js文件的script标签 
     *  
     **/
    public function getCombinedJsLink($combineAssets = true){
        if(empty($this->jsFiles)){
            return null;
        }  
        $inSiteScripts="";
        foreach($this->jsFiles as $file){ 
            $inSiteScripts .= sprintf('<script type="text/javascript" src="%s"></script>', WEBPATH.$file); 
        } 
        return $inSiteScripts; 
    }

    public function getCombinedCssLink($combineAssets=true){
        if(empty($this->cssFiles)){
            return null;
        } 
        $inSiteStyles="";
        foreach($this->cssFiles as $file){
            $ext=explode(".",$file);
            $ext=end($ext); 
            if($ext=='less')
                $inSiteStyles.= sprintf('<link rel="stylesheet/less" href="%s" />', WEBPATH.$file);
            else
                $inSiteStyles.= sprintf('<link rel="stylesheet" type="text/css" href="%s" />', WEBPATH.$file);
        } 
        return $inSiteStyles;
    }  
}
?>